<?php
class wp_spider_option{

	//架构函数
	public function __construct(){
		if(is_admin()){
			add_action('admin_init', array(&$this, 'spider_setting_init'));
			add_action('admin_menu', array(&$this, 'spider_menu'));
		}
	}

	//设置面板
	public function spider_menu(){
		define('SPIDER_URL', plugins_url('', __FILE__));
		//添加主目录
		add_menu_page('spider',
			'蜘蛛记录',
			'manage_options',
			'spider',
			array(&$this, 'spider_basic_intro'),
			SPIDER_URL.'/zz.jpg');
		//添加子目录
		add_submenu_page('spider',
			'spider',	
			'关键字设置',
			'manage_options',
			'basic_setting',
			array($this, 'spider_basic_setting'));
		add_submenu_page('spider',
			'spider',	
			'蜘蛛记录显示',
			'manage_options',
			'show_record',
			array($this, 'spider_show_record'));
	}

	//插件介绍
	public function spider_basic_intro(){
		echo file_get_contents(SPIDER_ROOT.'/spiderIntroduction.txt');
	}

	//设置初始化
	public function spider_setting_init(){
		register_setting('spider_options', 'spider_options', 'spider_options_validate');
		add_settings_section('spider_main', '关键字', array($this, 'spider_section'), 'spider');	
		//keyword
		//add_settings_field('keyword', 'keyword', array($this, 'spider_keyword'), 'wpcachedb', 'spider_main');
	}

	//关键字设置
	public function spider_section(){
		$key = get_option('spider_option');
		//var_dump($key);
		echo '<div><div>'.
			'<textarea name="keyword" style="width:500px;height:200px;" class="regular-text code">'.$key.
			'</textarea></div>'.
			'<div>例如:keyword|保存名|keyword2|保存名2</div></div>';
	}

	//关键字设置
	/*public function spider_keyword(){
		echo '<div>你好</div>';
	}*/

	//特殊设置(关键字)
	public function spider_basic_setting(){
		echo '<div class="wrap">';
		echo '<h2>'.'蜘蛛关键字设置'.'</h2>';
		echo '<div class="narrow">';
		echo '<form  method="post">';
			settings_fields('spider');
			do_settings_sections('spider');
		echo '<p class="submit"><input name="submit" type="submit" class="button-primary" value="保存设置" />';
		echo '<input style="margin-left:10px;" name="submit" type="submit" class="button-primary" value="清空记录" /></p>';
		echo '</form>';
		echo '</div>';
		echo '</div>';
	}

	//显示抓取记录
	public function spider_show_record(){
		global $wpdb, $spider;
		$table = $spider->tname;//表名

		$p = isset($_GET['p'])?$_GET['p']:'1';//当前页
		if((!is_numeric($p)) || $p<=0){$p = 1;}//页数的正确修改

		//总页数
		$sql = "select count(id) as count from {$table}";
		//echo $sql;
		$data = $wpdb->get_results($sql);
		$countNum = $data[0]->count;
		$pageNum = ceil($countNum/20);
		//总页数判断
		if($p>=$pageNum){$p=$pageNum;}



		$trTpl = "<tr><td width='25px'>%s</td><td width='80px'>%s</td><td width='118px'>%s</td><td width='60px'>%s</td><td>%s</td></tr>";
		$tableHeadTpl = sprintf($trTpl, '序号', '蜘蛛名', '时间', 'IP地址', '收录页');

		$tableBodyTpl = '';
		$data = $this->spider_data($p, $table);
		foreach($data as $k=>$v){
			$tableHeadTpl .= sprintf($trTpl, $v['id'], $v['name'], $v['time'], $v['ip'], $v['url']);
		}
		//var_dump($data);
		//echo($tableTpl);
		echo '<table class="wp-list-table widefat fixed posts">';
		echo '<thead>';
		echo($tableHeadTpl);
		echo '</thead>';
		echo '<tbody>';
		echo($tableBodyTpl);
		echo '</tbody>';
		echo($this->Pagination($p, $pageNum));
		echo '</table>';
		//echo $this->Pagination($p);
	}

	//蜘蛛抓取记录数据获取
	public function spider_data($page=1, $table='midoks_spider'){
		global $wpdb, $spider;
		$start = ($page-1)*20;//开始数据
		//var_dump($table);	
		$sql = "select * from {$table} order by id desc limit {$start},20";
		$data = $wpdb->get_results($sql);
		$newData = array();
		//echo '<pre>';
		foreach($data as $k=>$v){
			$arr = array();
			$arr['id'] = $v->id;
			$arr['name'] = $v->name;
			$arr['time'] = date('Y-m-d H:i:s', $v->time);
			$arr['ip'] = $v->ip;
			$arr['url'] = urldecode($v->url);
			$newData[] = $arr;
		}
		//var_dump($newData);
		return $newData;
	}

	//分页显示
	public function Pagination($page=1, $pageNum){	
		//当前页
		$url = $_SERVER['REQUEST_URI'];
		$r_url = str_replace(strstr($url, '&'), '', $url);
		//$r_url = $_SERVER['REQUEST_URI'];
		$thisPageUrl = 'http://'.$_SERVER['HTTP_HOST'].$r_url.'&p=';

		$string = '';
		$string .="总共{$pageNum}页/当前第{$page}页";
		if($pageNum>0){
			if($pageNum>1){
				$string .= "<span style='margin-left:5px;'><a href='".$thisPageUrl.'1'."#'>首页</a></span>";	
			}
			if($pageNum>2){
				$show=0;$showPrefix=0;
				$string .= "<span style='margin-left:5px;'><a href='".$thisPageUrl.($page-1)."#'><<</a></span>";
				for($i=2;$i<$pageNum;$i++){
					$url = $thisPageUrl.$i;
					if($i==$page){//当前页不连接
						$str = "<span style='margin-left:5px;'><a href='#' style='color:black;'>第{$i}页</a></span>";
					}else{
						$str = "<span style='margin-left:5px;'><a href='{$url}'>第{$i}页</a></span>";
					}

					if($pageNum>6){
						//前部分
						if(($showPrefix<3) && ($i > ($page-2)) && ($show<($pageNum-5))){
							$prefix .= $str;
							++$showPrefix;
						}//elseif(($showPrefix<3) && ($i > ($page-2))){
							//$prefix .= $str;
							//++$showPrefix;
						//}
						
						//后部分
						if($show>=($pageNum-5)){$suffix .= $str;}
					}else{
						$common .= $str;
					}	
					++$show;
				}
				if(!empty($common)){
					$string .= $common;
				}else{
					$middle = "<span style='margin-left:5px;'>...</span>";
					$string .= $prefix.$middle.$suffix;
				}
				$string .= "<span style='margin-left:5px;'><a href='".$thisPageUrl.($page+1)."#'>>></a></span>";
			}

			//尾页
			if($pageNum>2){
				$string .= "<span style='margin-left:5px;'><a href='".$thisPageUrl.$pageNum."#'>尾页</a></span>";	
			}
		}
		$pageTpl = '<tr><td colspan="5">%s</td></tr>';
		$content = sprintf($pageTpl, $string);
		//var_dump($page);
		return $content;
	}

}

function spider_options_validate($org){
	//var_dump($org);
}

if((isset($_POST['submit'])) && ($_POST['submit']=='清空记录') ){
	global $spider;
	$spider->clear();
}

//更新设置
if(isset($_POST['submit']) && isset($_POST['keyword'])){
	$key = $_POST['keyword'];
	//var_dump($key);
	update_option('spider_option', $key);
}
?>
